<?php

namespace App\Admin\Controllers;

use App\Models\Photo;
use App\Models\Category;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;


class PhotoController extends Controller
{
    use ModelForm;
    public $header = '图片';
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header($this->header);
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('编辑'.$this->header);
            $content->description('图片');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * 创建组织
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('创建'.$this->header);

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(photo::class, function (Grid $grid) {


            $grid->id('ID')->sortable();
            $grid->title('标题');
            $grid->path('图片')->image(env('BASE_URL'),100,100);
//            1是公告2是会议记录3是营销政策4是考勤公告
//            $grid->video('视频地址')->video(['videoWidth' => 720, 'videoHeight' => 480]);
//            $grid->column('type','类型')->using([
//                '1'=>'公告',
//                '2'=>'会议',
//                '3'=>'营销',
//                '4'=>'考勤'
//            ]);
            $status = [
                'on'  => ['value' => 1, 'text' => '开启', 'color' => 'success'],
                'off' => ['value' => 2, 'text' => '关闭', 'color' => 'danger'],
            ];
            $grid->status('状态')->switch($status);
            $grid->category('分类')->title('分类');
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableView();
//                $actions->disableDelete();
//                $actions->disableEdit();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(photo::class, function (Form $form) {
            $form->tab('图片发布', function ($form) {

                $form->display('id', '图片ID');
//                $form->select('category_id','分类id')->options(Category::where(['type'=>1 , 'status'=>1])->pluck('title','id'))->rules('required');
                $form->select('category_id','分类id')->options(Category::selectOptions(
                    function ($query) {
                        return $query->where(['type'=>2 , 'status'=>1]);
                    },'无'))->rules('required');
                $form->text('title','标题')->rules('required');
                $form->image('path','图片')->uniqueName();
                $form->number('sort','排序(序号小的在前)')->default(100);
                $form->switch('status', '状态')->states([
                    'on'  => ['value' => 1, 'text' => '开启', 'color' => 'success'],
                    'off' => ['value' => 2, 'text' => '关闭', 'color' => 'danger'],
                ])->default(1);
            });
        });
    }
}
